from selenium import webdriver
from time import sleep
driver=webdriver.Firefox()
driver.get('https://www.baidu.com/')
sleep(2)
bt=driver.find_element_by_partial_link_text('新闻')
bt.click()
sleep(2)
HDS=driver.window_handles
print(HDS)

driver.switch_to_window(HDS[-1])
print(driver.current_window_handle)
sleep(2)

dian1=driver.find_element_by_partial_link_text('军事')
dian1.click()
sleep(2)
driver.switch_to_window(HDS[-1])
print(driver.current_window_handle)
sleep(2)

dian2=driver.find_element_by_partial_link_text('中国')
dian2.click()
sleep(2)

#切换并关闭打开的界面
driver.switch_to_window(HDS[-1])
driver.close()
sleep(1)

HDS=driver.window_handles
driver.switch_to_window(HDS[-1])
driver.close()
sleep(1)
# 切回首页

HDS=driver.window_handles
driver.switch_to_window(HDS[-1])
sleep(2)
key_word = driver.find_element_by_id('kw')

key_word.send_keys('selenium')
sleep(2)
bt1 = driver.find_element_by_id('su')

bt1.click()

sleep(3)

driver.quit()




